<?php

    // +----------------------------------------------------------------------
    // | 属性控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\event\cms;

    use app\common\event\Base;
    use think\Config;

    class Pattern extends Base
    {

        public static function getModelBuilderGroup($id)
        {
            $model = self::get($id);
            $group = [];
            if ($model && is_array($model->field_group_show)) {
                foreach ($model->field_group_show as $key => $value) {
                    dump($model->field_sort[$key]);
                    if ($model->extend) {
                        $field = Attribute::where('pattern_id', 'in', $model->id . ',' . $model->extend)->where('id', 'in', $model->field_sort[$key])->column('name');
                        //                    $field = Attribute::where('pattern_id','in',$model->id.','.$model->extend)->order('field(id,'.explode($model->field_sort[$key],',').')')->where('id','in',$model->field_sort[$key])->column('name');
                    } else {
                        $field = Attribute::where('pattern_id', $model->id)->where('id', 'in', $model->field_sort[$key])->column('name');
                    }
                    $group[$value] = $field;
                };
            }
            return $group;
        }

        /**
         * 根据数据表生成模型及其属性数据.
         */
        public function generate($table, $name = '', $title = '')
        {
            // 新增模型数据
            if (empty($name)) {
                $name = $title = substr($table, strlen(Config::get('database.prefix')));
            }
            $data = [
                'name' => $name,
                'title' => $title,
            ];
            $data = $this->create($data);
            if ($data) {
                $res = $this->add($data);
                if (!$res) {
                    return false;
                }
            } else {
                $this->error = $this->getError();
                return false;
            }
            // 新增属性
            $fields = M()->query('SHOW FULL COLUMNS FROM ' . $table);
            foreach ($fields as $key => $value) {
                $value = array_change_key_case($value);
                // 不新增id字段
                if (strcmp($value['field'], 'id') == 0) {
                    continue;
                }
                // 生成属性数据
                $data = [];
                $data['name'] = $value['field'];
                $data['title'] = $value['comment'];
                $data['type'] = 'string'; // TODO:根据字段定义生成合适的数据类型
                // 获取字段定义
                $is_null = strcmp($value['null'], 'NO') == 0 ? ' NOT NULL ' : ' NULL ';
                $data['field'] = $value['type'] . $is_null;
                $data['value'] = $value['default'] == null ? '' : $value['default'];
                $data['pattern_id'] = $res;
                $_POST = $data; // 便于自动验证
                D('Attribute')->update($data, false);
            }
            return $res;
        }

        /**
         * 删除一个模型.
         *
         * @param int $id 模型id
         */
        public function del($id)
        {
            // 获取表名
            $model = $this->field('name,extend')->find($id);
            if ($model['extend'] == 0) {
                $table_name = Config::get('database.prefix') . strtolower($model['name']);
            } elseif ($model['extend'] == 1) {
                $table_name = Config::get('database.prefix') . 'document_' . strtolower($model['name']);
            } else {
                $this->error = '只支持删除文档模型和独立模型';
                return false;
            }
            // 删除属性数据
            M('Attribute')->where([
                'pattern_id' => $id,
            ])->delete();
            // 删除模型数据
            $this->delete($id);
            // 删除该表
            $sql = <<<sql
                DROP TABLE {$table_name};
sql;
            $res = M()->execute($sql);
            return $res !== false;
        }
    }
